package com.atstudy.headline.web.admin.controller.baseDicts;

import com.atstudy.headline.entity.BaseDicts;
import com.atstudy.headline.entity.Users;
import com.atstudy.headline.enums.BaseStatus;
import com.atstudy.headline.result.Result;
import com.atstudy.headline.web.admin.service.BaseDictsService;
import com.atstudy.headline.web.admin.vo.baseDicts.BaseDictInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/dict")
public class DictsController {
    @Autowired
    private BaseDictsService baseDictsService;

    /**
     *
     * @param current
     * @param size
     * @param dictInfoVo
     * @return
     */
    @RequestMapping("/page")
    public Result<IPage<BaseDicts>> pageDictInfo(@RequestParam Integer current,
                                                 @RequestParam Integer size,
                                                 BaseDictInfoVo dictInfoVo){
        Page<BaseDicts> page = new Page<>(current,size);
        LambdaQueryWrapper<BaseDicts> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like( dictInfoVo.getDictName()!=null, BaseDicts::getDictName,dictInfoVo.getDictName());
        queryWrapper.eq( dictInfoVo.getDictType()!=null,BaseDicts::getDictType,dictInfoVo.getDictType());
        IPage<BaseDicts> dictIPage = baseDictsService.page(page,queryWrapper);
        return Result.ok(dictIPage);
    }

    /**
     * 字典分类
     * @return
     */
    @GetMapping("/types")
    public Result<List<String>> getDictTypes() {
        List<String> types = baseDictsService.getDictTypes();
        return Result.ok(types);
    }

    /**
     * 增加/修改
     * @param dicts
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody BaseDicts dicts){
        baseDictsService.saveOrUpdate(dicts);
        return Result.ok();
    }

    /**
     * 删除
     * @param dictId
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer dictId) {
        baseDictsService.removeById(dictId);
        return Result.ok();
    }

    /**
     * 根据id修改状态
     * @param dictId
     * @param status
     * @return
     */
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestParam Integer dictId  ,@RequestParam BaseStatus status) {
        LambdaUpdateWrapper<BaseDicts> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BaseDicts::getDictId,dictId);
        updateWrapper.set(BaseDicts::getIsActive,status);
        baseDictsService.update(updateWrapper);
        return Result.ok();
    }

    /**
     *查询广告类型
     * @return
     */
    @GetMapping("/dropdown")
    public Result<List<Map<Integer, Object>>> getAdvertiser() {
        List<Map<Integer, Object>> dropdownList = baseDictsService.selectAdType();
        return Result.ok(dropdownList);
    }
}
